package servlet;

import com.alibaba.fastjson.JSON;
import entity.Wallet;
import service.WalletService;
import service.WalletTypeService;
import service.impl.WalletServiceImpl;
import service.impl.WalletTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付钱包
 */
@WebServlet("/WalletServlet")
public class WalletServlet extends HttpServlet {
    ResultSet rs;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        WalletService walletService = new WalletServiceImpl();
        Wallet wallet = new Wallet();

        List list = new ArrayList();
        String listString = "";

        String opr = req.getParameter("opr");
        if (opr.equals("wallet")){
            int uid = Integer.parseInt(req.getParameter("uid"));
            rs = walletService.selectWallet(uid);
            try{
                while (rs.next()){
                    wallet = new Wallet();

                    int wtid = rs.getInt("wtid");
                    String wtname = rs.getString("wtname");
                    int wid = rs.getInt("wid");
                    uid = rs.getInt("uid");
                    Double wamount = rs.getDouble("wamount");
                    String wname = rs.getString("wname");

                    wallet.setWtid(wtid);
                    wallet.setWtname(wtname);
                    wallet.setWid(wid);
                    wallet.setUid(uid);
                    wallet.setWamount(wamount);
                    wallet.setWname(wname);

                    list.add(wallet);
                }
            }catch (Exception e){
                e.printStackTrace();
            }

            listString = JSON.toJSONString(list);
            out.print(listString);
        }else if (opr.equals("upd")){
            int wid = Integer.parseInt(req.getParameter("wid"));
            int sid = Integer.parseInt(req.getParameter("sid"));
            double amount = Double.parseDouble(req.getParameter("amount"));
            String wname = req.getParameter("wname");
            if (sid == 1){
                amount = 0 - amount;
            }

            //先根据wid查询出钱包信息
            walletService.selectWalletByWid(wid);
            try {
                while (rs.next()){
                    int wtid = rs.getInt("wtid");
                    String wtname = rs.getString("wtname");
                    wid = rs.getInt("wid");
                    int uid = rs.getInt("uid");
                    Double wamount = rs.getDouble("wamount");
                    wname = rs.getString("wname");

                    wallet.setWtid(wtid);
                    wallet.setWtname(wtname);
                    wallet.setWid(wid);
                    wallet.setUid(uid);
                    wallet.setWamount(wamount);
                    wallet.setWname(wname);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            //按照改值修改wallet对象的值
            double wamount = wallet.getWamount() + amount;
            wallet.setWamount(wamount);
            wallet.setWname(wname);
            //将修改的值传入数据库，完成修改
            walletService.updateWallet(wallet);

        }else if (opr.equals("walletByWid")){
            Map map = new HashMap();
            int wid = Integer.parseInt(req.getParameter("wid"));
            ResultSet rs = walletService.selectWalletByWid(wid);
            try {
                while (rs.next()){
                    int wtid = rs.getInt("wtid");
                    String wtname = rs.getString("wtname");
                    wid = rs.getInt("wid");
                    int uid = rs.getInt("uid");
                    Double wamount = rs.getDouble("wamount");
                    String wname = rs.getString("wname");

                    wallet.setWtid(wtid);
                    wallet.setWtname(wtname);
                    wallet.setWid(wid);
                    wallet.setUid(uid);
                    wallet.setWamount(wamount);
                    wallet.setWname(wname);
                    map.put("wallet",wallet);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            String JsonStr = JSON.toJSONString(map);
            out.print(JsonStr);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
